//
//  InvationCode.m
//  YYPHSpeedJump
//
//  Created by sujiang on 16/1/11.
//  Copyright © 2016年 sujiang. All rights reserved.
//

#import "InvationCode.h"
#import "RegistViewController.h"
#import "ErWeiMa.h"

@interface InvationCode ()
{
    UIView *baseView;
    ErWeiMa *aView;
    CGRect frame;
}
@end

@implementation InvationCode

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addSubViews];
    self.title = @"输入邀请码";
}
- (void)viewWillDisappear:(BOOL)animated
{
    [self.view endEditing:YES];
}

- (void)addSubViews
{
    self.invationTF = [[PasswordTextField alloc] initWithFrame:CGRectMake(0, 10, kScreenWidth, 44)];
    self.invationTF.placeholder = @"请输入邀请码";
    [self.view addSubview:self.invationTF];
    self.invationTF.backgroundColor = kColors(255, 255, 255, 1);
    self.view.backgroundColor = kColors(231, 231, 231, 1);
    self.invationTF.font = [UIFont systemFontOfSize:12];
    
//    UIView *bView = [[UIView alloc] initWithFrame:CGRectMake(self.invationTF.right, 10, 96, 44)];
//    [self.view addSubview:bView];
//    bView.backgroundColor = [UIColor whiteColor];
    
//    UIButton *getBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
//    getBtn.frame = CGRectMake(0, (self.invationTF.height - 29) / 2, 81, 29);
//    getBtn.titleLabel.font = [UIFont systemFontOfSize:12];
//    [getBtn setTitleColor:kColors(255, 255, 255, 1) forState:(UIControlStateNormal)];
//    getBtn.layer.cornerRadius = 5;
//    [bView addSubview:getBtn];
//    [getBtn setTitle:@"获取邀请码" forState:(UIControlStateNormal)];
//    getBtn.backgroundColor = kColors(218, 166, 46, 1);
//    [getBtn addTarget:self action:@selector(actionGetCode:) forControlEvents:(UIControlEventTouchUpInside)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"fanhui"] style:(UIBarButtonItemStylePlain) target:self action:@selector(actionBack)];
    
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.frame = CGRectMake(22, self.invationTF.bottom + 18, kScreenWidth - 40, 44);
    [self.view addSubview:button];
    [button setBackgroundImage:[UIImage imageNamed:@"xybl"] forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(actionNext:) forControlEvents:(UIControlEventTouchUpInside)];
    aView = [[ErWeiMa alloc] initWithFrame:CGRectMake(13, button.bottom + 52, kScreenWidth - 26, 227 *kHeightScale)];
    [self.view addSubview:aView];
    aView.backBtn.hidden = YES;
    UITapGestureRecognizer *tapWX = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionTapWX)];
    [aView.wxImageView addGestureRecognizer:tapWX];
    UITapGestureRecognizer *tapWB = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionTapWB)];
    [aView.wbImageView addGestureRecognizer:tapWB];
    
}


#pragma mark -- 按钮点击事件

- (void)actionTapWX
{
    [self zoomImageView:aView.wxImageView];
}

- (void)actionTapWB
{
    [self zoomImageView:aView.wbImageView];
}

- (void)zoomImageView:(UIImageView *)imageView
{
    [self.view endEditing:YES];
    frame = imageView.frame;
    baseView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    baseView.backgroundColor = [UIColor blackColor];
    baseView.alpha = 0.8;
    [self.view.window addSubview:baseView];
    [imageView removeFromSuperview];
    [baseView addSubview:imageView];
    imageView.center = CGPointMake(baseView.center.x, baseView.center.y);
    imageView.transform = CGAffineTransformScale(imageView.transform, 2, 2);
   
    UITapGestureRecognizer *tapCancle = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionCancle:)];
    [baseView addGestureRecognizer:tapCancle];
   

}

- (void)actionCancle:(UITapGestureRecognizer *)tap
{
    baseView.subviews.firstObject.frame = frame;
    [aView.aView addSubview:baseView.subviews.firstObject];
    [baseView removeFromSuperview];
}


- (void)actionGetCode:(UIButton *)button
{
//    [self.view endEditing:YES];
//    baseView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
//    baseView.backgroundColor = [UIColor grayColor];
//    baseView.alpha = 0.5;
//    [self.view.window addSubview:baseView];
//    
//    aView = [[ErWeiMa alloc] initWithFrame:CGRectMake(13, (kScreenHeight - 227 *kHeightScale) / 2, kScreenWidth - 26, 227 *kHeightScale)];
//    [self.view.window addSubview:aView];
//    [aView.backBtn addTarget:self action:@selector(actionBack:) forControlEvents:(UIControlEventTouchUpInside)];
}

- (void)actionBack:(UIButton *)button
{
    [baseView removeFromSuperview];
    [aView removeFromSuperview];
}

- (void)actionNext:(UIButton *)button
{
    if (self.invationTF.text.length > 0) {
        
        // /app.do?cmd=valid_invite_code&invite_code=XXXXX
        NSString *string = [NSString stringWithFormat:@"%@%@", kBaseUrl, kInvation];
        NSDictionary *dict = @{@"invite_code":self.invationTF.text};
        [NetworkHandle getOrderInfoWithUrl:string dictionary:dict success:^(NSDictionary *dictionary) {
            if ([dictionary[@"code"] integerValue] == 200) {
                RegistViewController *registVC = [[RegistViewController alloc] init];
                registVC.code = self.invationTF.text;
                [self.navigationController pushViewController:registVC animated:YES];
            } else {
                [Tools showAlert:dictionary[@"message"]];
            }
        }];
        
    } else {
        [Tools showAlert:@"邀请码不能为空!"];
        return;
    }
}

- (void)actionBack
{
    [self dismissViewControllerAnimated:YES completion:nil];
}



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.invationTF resignFirstResponder];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
